package com.plusone.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.plusone.action.ContactAction;
import com.plusone.model.Contact;
import com.plusone.toolset.InputVerifier;

public class AboutusController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		ServletContext context = getServletContext();

		RequestDispatcher dispatcher = context
				.getRequestDispatcher("/view/aboutus.jsp");
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("name-field");
		String email = request.getParameter("email-field");
		String phone = request.getParameter("phone-field");

		Contact contact = new Contact();
		contact.setDisplayName(name);
		contact.setEmailAddress(email);
		contact.setPhoneNumber(phone);
		contact.setIpaddress(request.getRemoteAddr());

		ContactAction action = new ContactAction(new InputVerifier());

		if (action.IsValid(contact)) {
			action.Save(contact);
			action.sendMail(contact);
		} else {
			// response.setContentType("text/plain");
			// response.setStatus(400);// bad request
		}
	}
}
